import QtQuick 1.0

import com.nokia.symbian 1.0

Window {
    id: mainwindow
    property int selected: 0

    StatusBar {
        id: statusBar
        anchors.top: mainwindow.top
    }
    Text {
        id: appTitle
        text: "TED Tv"
        width: parent.width
        color: "red"
        font.bold: true
        horizontalAlignment: Text.AlignHCenter
        font.pixelSize: 35
        anchors.top: statusBar.bottom
    }
    BusyIndicator {
         id: indicator
         running: true
         width: 50
         height: 50
         anchors.right: mainwindow.right
         anchors.top : statusBar.bottom
    }
    XmlListModel {
        id: xmlModel
        source: "http://www.ted.com/talks/rss"
        query: "/rss/channel/item"

        //namespaceDeclarations: "declare default element namespace 'http://www.w3.org/2005/Atom';"
        namespaceDeclarations: "declare namespace media=\"http://search.yahoo.com/mrss/\";"
        XmlRole { name: "title"; query: "title/string()" }
        XmlRole { name: "desc"; query: "description/string()" }
        XmlRole { name: "pubDate"; query: "pubDate/string()" }
        XmlRole { name: "thumbnail"; query: 'media:thumbnail/@url/string()' }
        XmlRole { name: "video"; query: 'media:content/@url/string()' }

        onStatusChanged: {
            if(status == XmlListModel.Ready) {
                indicator.running = false;
                indicator.visible = false;
            }
        }

    }
    PageStack {
        id: pagestk
    }

    Component.onCompleted: {
        pagestk.push(page1);
    }

    Page {
        id : page2
        anchors.top: appTitle.bottom
        width : parent.width
        height: parent.height - toolBar.height -statusBar.height
        Flickable {
            id:fl
            flickableDirection: Flickable.VerticalFlick
            boundsBehavior: Flickable.StopAtBounds
            contentHeight: sharebtn.y + sharebtn.height
            contentWidth: page2.width
            //clip : true

            Text {
                    id: pageTitle
                    text: xmlModel.get(selected).title
                    color : "white"
                    width : page2.width
                    height: 50
                    anchors.topMargin: 30
                    wrapMode: Text.WordWrap
            }
            Image {
                id :thumb
                source : xmlModel.get(selected).thumbnail
                anchors.top: pageTitle.bottom
                anchors.topMargin: 40
                width : sourceSize.width
                height: sourceSize.height
                anchors.horizontalCenter: parent.horizontalCenter

                MouseArea {
                    anchors.fill: parent
                    onClicked:  {
                        console.debug("Clicked");
                        Qt.openUrlExternally(xmlModel.get(selected).video );
                    }
                }

            }
            Text {
                id: pageDesc
                text: xmlModel.get(selected).desc
                color : "lightgrey"
                width : parent.width
                height: 100
                anchors.top: thumb.bottom
                horizontalAlignment : Text.AlignLeft
                anchors.topMargin:50
                wrapMode: Text.WordWrap
            }
            Image {
                id:sharebtn
                source : "share.png"
                width : sourceSize.width
                height : sourceSize.height
                y: pageDesc.y + pageDesc.paintedHeight + 20
                //anchors.top:
                //anchors.topMargin: 40
                anchors.horizontalCenter: parent.horizontalCenter



                MouseArea {
                    anchors.fill: sharebtn
                    onClicked : {
                        console.debug("X" + mouseX + " ," + xmlModel.get(selected).video )
                        if(mouseX > 100 && mouseX < 150) {
                            Qt.openUrlExternally("http://www.facebook.com/sharer.php?u="+xmlModel.get(selected).video +"&t=SAMPLE" );
                        }
                        else if(mouseX >= 150) {

                            Qt.openUrlExternally("http://twitter.com/home?status=Watching%20"+xmlModel.get(selected).video );
                        }
                    }
                }
            }


        }
        ScrollBar {
            flickableItem: fl
            anchors.right: fl.right
            anchors.top: fl.top
            visible : true

        }

        onStatusChanged:  {
            if(status == PageStatus.Active) {
                console.debug("Ct ht" + fl.contentHeight )
                 console.debug("into page 2" + xmlModel.get(selected).title )
                console.debug("y" + sharebtn.y)
            }
        }



    }

    Page {
        id: page1
        anchors.top: appTitle.bottom
        width : parent.width
        height: parent.height - toolBar.height -statusBar.height

        Component {
            id: listDelegate
            Item {
                width: parent.width
                height: elementTitle.height + elementTime.height + 2

                Image {
                    id: elementImage
                    source: thumbnail
                    width: 60
                    height: 60
                }

                Text {
                    id: elementTitle
                    text: title
                    width: parent.width - elementImage.width
                    color: "white"
                    wrapMode: Text.WordWrap
                    anchors.left: elementImage.right
                    anchors.leftMargin: 15
                }
                Text {
                    id: elementTime
                    text: pubDate
                    color:  "grey"
                    anchors.left: elementImage.right
                    anchors.top: elementTitle.bottom
                    anchors.leftMargin: 15
                    font.pixelSize: 12
                }

                Rectangle {
                    id: blueline
                    width: parent.width
                    height: 2
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.top: parent.top
                    color: "navy"
                }

                MouseArea{
                    anchors.fill :parent
                    onClicked : {
                        console.log("item title: " + title);

                        selected = index;
                        console.log("item clicked: " + selected);
                        pagestk.push(page2)
                        //console.log("pgStk" + page1.pageStack.find(page2))
                    }

                }

            }
        }



        ListView {
            id : listid
            width: page1.width
            height: page1.height
            model: xmlModel
            anchors.top: page1.top
            delegate: listDelegate

            highlight : Rectangle {
                color: "darkgrey"
                radius: 5
                width: 600 }
            focus: true
            clip: true

        }
    }
    ToolBar {
        id: toolBar
        anchors.bottom: mainwindow.bottom;
        tools: ToolBarLayout {
            id: toolBarLayout
            ToolButton {
                flat: true
                iconSource: (pagestk.currentPage == page1)?"quit.png":"toolbar-back"
                onClicked: {
                    if(pagestk.currentPage == page1) {
                        console.debug("Quiting")
                         Qt.quit()
                    }
                    else {
                        console.debug("back")
                        pagestk.pop();
                    }
                }
            }
            ToolButton {
                flat : true
                iconSource : "info.png"
                onClicked: infoDialog.open()
            }
        }
    }
    Dialog {
        id : infoDialog
        height: 300
        width : 300

        content: Item {
            id: dialogContent
            Text {
                height : 80
                width : 300
                color: "lightgrey"
                wrapMode: Text.WordWrap
                horizontalAlignment: Text.AlignHCenter
                text : "This is a Open Source project hosted at Forum Nokia projects. Project Owners : Manikantan.K & Sunil Kumar"
            }

        }
        status: DialogStatus.Closed
        onClickedOutside: infoDialog.close()
    }
}
